package com.example.designpattern.singleExample;

import java.io.Serializable;

/**
 * @program: DesignPattern
 * @description: 解决序列化破坏单例对象
 * @author: zhangjt
 * @create: 2024-11-14 15:22
 **/
public class NoSerializationDulLazyCreateBean implements Serializable {
    private static volatile NoSerializationDulLazyCreateBean instance;

    private NoSerializationDulLazyCreateBean(){
        if (instance != null){
            throw new RuntimeException("单例不允许多个实例");
        }
    }

    public static NoSerializationDulLazyCreateBean getInstance(){
        if(instance == null){
            synchronized (NoSerializationDulLazyCreateBean.class){
                if(instance == null){
                    instance = new NoSerializationDulLazyCreateBean();
                }
            }
        }
        return instance;
    }
    private Object readResolve() {
        return NoSerializationDulLazyCreateBean.getInstance();
    }
}
